package Control;

import Database.*;
import Model.*;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;

import oracle.jdbc.dcn.TableChangeDescription.TableOperation;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.ArrayList;

public class DeleteCustomer extends JFrame implements ActionListener {
	private JLabel id;
	private JTextField name;
	private JButton search, clear, delete, cancel;
	private JPanel leftPanel, rightPanel;
	private JTextArea names;
	private TableOperations to;
	private JTable jt;
	private DefaultTableModel jtM;
	private ArrayList users;
	private ArrayList<LoyaltyCustomer> test = new ArrayList<LoyaltyCustomer>();
	private LoyaltyCustomer toBeEdited = null;
	private ImageIcon icon = new ImageIcon("frameIcon.png");

	public DeleteCustomer() {
		GridBagLayout layout = new GridBagLayout();
		GridBagConstraints gc = new GridBagConstraints();

		setIconImage(icon.getImage());
		setTitle("MDL - Delete Customer");
		setSize(450, 375);
		setLocationRelativeTo(null);
		setResizable(false);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setLayout(layout);
		to = new TableOperations();

		Insets insets = new Insets(15, 15, 13, 3);

		id = new JLabel("ID or Name");
		gc.gridx = 0;
		gc.gridy = 0;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		add(id, gc);

		name = new JTextField(10);
		gc.gridx = 0;
		gc.gridy = 0;
		gc.weighty = 0;
		gc.weightx = 1;
		insets = new Insets(15, 100, 13, 15);
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		add(name, gc);

		search = new JButton("Search");
		search.addActionListener(this);
		gc.gridx = 0;
		gc.gridy = 1;
		gc.weighty = 0;
		gc.weightx = 1;
		insets = new Insets(0, 25, 0, 250);
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		add(search, gc);

		clear = new JButton("Clear");
		clear.addActionListener(this);
		gc.gridx = 0;
		gc.gridy = 1;
		gc.weighty = 0;
		gc.weightx = 1;
		insets = new Insets(0, 250, 0, 25);
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		add(clear, gc);

		gc.gridx = 0;
		gc.gridy = 2;
		gc.weighty = 0;
		gc.weightx = 0;
		insets = new Insets(15, 50, 0, 3);
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		add(new JLabel("Users"), gc);

		createTable();
		gc.gridx = 0;
		gc.gridy = 3;
		gc.weighty = 1;
		gc.weightx = 3;
		insets = new Insets(5, 25, 15, 30);
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		add(new JScrollPane(jt), gc);

		delete = new JButton("Delete");
		delete.addActionListener(this);
		gc.gridx = 0;
		gc.gridy = 4;
		gc.weighty = 0;
		gc.weightx = 1;
		insets = new Insets(0, 25, 25, 250);
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		add(delete, gc);

		cancel = new JButton("Cancel");
		cancel.addActionListener(this);
		gc.gridx = 0;
		gc.gridy = 4;
		gc.weighty = 0;
		gc.weightx = 1;
		insets = new Insets(0, 250, 25, 25);
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		add(cancel, gc);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == cancel)
			this.dispose();
		else if (e.getSource() == search) {
			if (jtM.getRowCount() > 0) {
				clear();
				search();
			} else
				search();
		} else if (e.getSource() == clear)
			clear();
		else if (e.getSource() == delete) {
			int del = JOptionPane.showConfirmDialog(
					null,
					"Are you sure you want to delete Customer "
							+ toBeEdited.getName(), "Confirm delete",
					JOptionPane.YES_NO_CANCEL_OPTION,
					JOptionPane.QUESTION_MESSAGE);
			if (del == JOptionPane.YES_OPTION) {
				if (to.deleteCustomer(toBeEdited)) {
					JOptionPane.showMessageDialog(null, "Customer Deleted");
					clear();
				} else
					JOptionPane.showMessageDialog(null,
							"Customer could not be deleted");
			}
		}
	}

	public void createTable() {
		jtM = new DefaultTableModel();

		jtM.addColumn("Customer ID");
		jtM.addColumn("Name");

		fillTable();

		jt = new JTable(jtM);
		jt.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				int row = jt.getSelectedRow();
				int custID = test.get(row).getCustID();
				String name = test.get(row).getName();
				String address = test.get(row).getAddress();
				String phone = test.get(row).getPhone();
				String email = test.get(row).getEmail();
				java.sql.Date date = test.get(row).getInDate();
				int points = test.get(row).getPoints();
				toBeEdited = new LoyaltyCustomer(custID, name, address, phone,
						email, date, points);
				System.out.println(custID);
			}
		});
	}

	public void clear() {
		try {
			jtM.removeRow(0);
			for (int i = 0; i < jtM.getRowCount(); i++) {
				jtM.removeRow(i);
			}

			test.remove(0);
			for (int i = 0; i < test.size(); i++) {
				test.remove(i);
			}
		} catch (Exception e) {
			System.out.println("No rows to be deleted");
		}
	}

	public void fillTable() {
		if (jtM.getRowCount() >= 1) {
			clear();

			String[] name = {
					Integer.toString(test.get(test.size() - 1).getCustID()),
					test.get(test.size() - 1).getName() };
			jtM.addRow(name);

		} else {
			for (int i = 0; i < test.size(); i++) {
				String[] name = { Integer.toString(test.get(i).getCustID()),
						test.get(i).getName() };
				jtM.addRow(name);
			}
		}
	}

	public void search() {
		try {
			test.addAll(to.searchCustomer(Integer.parseInt(name.getText())));
		} catch (NumberFormatException nfe) {
			test.addAll(to.searchCustomer(name.getText()));
		}
		fillTable();
		if (jtM.getRowCount() <= 0)
			JOptionPane
					.showMessageDialog(null,
							"Customer not found. Hint: Use a CAPITAL LETTER for the 1st Letter");
	}
}